home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / expire / histinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-12  |  3.0 KB  |  155 lines

  1. /*
  2.  * histinfo - print history file lines for articles named on stdin
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>        /* for modified time (date received) */
  8. #include <string.h>
  9. #include "config.h"
  10. #include "fgetmfs.h"
  11. #include "alloc.h"
  12. #include "case.h"
  13.  
  14. #define STRLEN(s) (sizeof(s) - 1)    /* s must be a char array */
  15.  
  16. char *progname;
  17. int debug;
  18.  
  19. FILE *efopen();
  20.  
  21. char *spdir;
  22. int spdirlen;
  23.  
  24. /*
  25.  * main - parse arguments and handle options
  26.  */
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     int c;
  32.     int errflg = 0;
  33.     FILE *in;
  34.     char *inname;
  35.     register int i;
  36.     extern int optind;
  37.     extern char *optarg;
  38.  
  39.     progname = argv[0];
  40.     while ((c = getopt(argc, argv, "d")) != EOF)
  41.         switch (c) {
  42.         case 'd':
  43.             ++debug;
  44.             break;
  45.         default:
  46.             errflg++;
  47.             break;
  48.         }
  49.     if (optind < argc || errflg) {
  50.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  51.         exit(2);
  52.     }
  53.  
  54.     spdir = artfile((char *)NULL);
  55.     spdirlen = strlen(spdir);
  56.     
  57.     while ((inname = fgetms(stdin)) != NULL) {
  58.         i = strlen(inname);
  59.         if (i > 0)
  60.             inname[i-1] = '\0';    /* kill newline */
  61.         if (strchr(inname, '.') == NULL) {    /* skip dot names */
  62.             in = efopen(inname, "r");
  63.             process(in, inname);
  64.             (void) fclose(in);
  65.         }
  66.         free(inname);
  67.     }
  68.     exit(0);
  69. }
  70.  
  71. /*
  72.  * process - process input file
  73.  */
  74. process(in, inname)
  75. FILE *in;
  76. char *inname;
  77. {
  78.     char *name;
  79.     char *line;
  80.     char *msgid;
  81.     char *expiry;
  82.     time_t datercv;
  83.     struct stat statb;
  84.     static char msgidnm[] =  "Message-ID:";
  85.     static char expnm[] =    "Expires:";
  86.     register char *p;
  87.     register int i;
  88.  
  89.     expiry = strsave("-");
  90.     msgid = NULL;
  91.  
  92.     /* read until EOF or blank line (end of headers) */
  93.     while ((line = fgetms(in)) != NULL && strcmp(line, "\n") != 0) {
  94.         i = strlen(line);
  95.         if (i > 0)
  96.             line[i-1] = '\0';        /* trim newline */
  97.         if (CISTREQN(line, msgidnm, STRLEN(msgidnm))) {
  98.             if (msgid != NULL)
  99.                 free(msgid);
  100.             p = line + STRLEN(msgidnm);
  101.             msgid = strsave(p + strspn(p, " \t"));
  102.         } else if (CISTREQN(line, expnm, STRLEN(expnm))) {
  103.             free(expiry);
  104.             p = line + STRLEN(expnm);
  105.             expiry = strsave(p + strspn(p, " \t"));
  106.         }
  107.         free(line);
  108.     }
  109.     if (line != NULL)
  110.         free(line);
  111.  
  112.     /* generate the file name */
  113.     name = inname;
  114.     if (strncmp(name, spdir, spdirlen) == 0 &&
  115.         name[spdirlen] == '/')
  116.         name += spdirlen + 1;    /* skip spool dir. & slash */
  117.  
  118.     /* generate the date received */
  119.     (void) fstat(fileno(in), &statb);
  120.     datercv = statb.st_mtime;
  121.  
  122.     /* deal with empty and trash articles */
  123.     if (msgid == NULL || strchr(msgid, '\t') != NULL || msgid[0] != '<' ||
  124.                     msgid[strlen(msgid)-1] != '>' ||
  125.                     strchr(expiry, '\t') != NULL ||
  126.                     strchr(expiry, '~') != NULL) {
  127.         if (msgid != NULL)
  128.             free(msgid);
  129.         msgid = str3save("<", name, "@trash>");
  130.         datercv = 0;
  131.         free(expiry);
  132.         expiry = strsave("0");
  133.     }
  134.  
  135.     /* whomp out the history line */
  136.     (void) fputs(msgid, stdout);
  137.     printf("\t%ld~", (long)datercv);
  138.     (void) fputs(expiry, stdout);
  139.     (void) putchar('\t');
  140.     (void) fputs(name, stdout);
  141.     (void) putchar('\n');
  142.     (void) fflush(stdout);
  143.     free(msgid);
  144.     free(expiry);
  145. }
  146.  
  147. /*
  148.  * unprivileged - no-op to keep pathname stuff happy
  149.  */
  150. void
  151. unprivileged(reason)
  152. char *reason;
  153. {
  154. }
  155.